import type { PaymentMethod } from '@/enums'
import type {
  ConsultOrderPreData,
  ConsultOrderPreParams,
  Dep,
  DoctorPage,
  FollowType,
  Image,
  KnowledgePage,
  KnowledgeParams,
  PageParams,
  PartialConsult
} from '@/types/consult'
import { request } from '@/utils/request'

// 获取支付地址
export const getPayUrl = (params: {
  paymentMethod: PaymentMethod
  orderId: string
  payCallback: string
}) => {
  return request<{ payUrl: string }>(
    '/patient/consult/pay',
    'POST',
    params
  ).then((res) => res.data.payUrl)
}

// 生成订单
export const createConsultOrder = (data: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', data).then(
    (res) => res.data.id
  )

// 拉取预支付订单信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>(
    '/patient/consult/order/pre',
    'GET',
    params
  ).then((res) => res.data)

/**
 * 上传图片
 * @param file 图片文件对象
 */
export const uploadImage = (file: File) => {
  const fd = new FormData()
  fd.append('file', file)
  return request<Image>('/upload', 'POST', fd)
}

/**
 * 找医生--查询所有科室-层级
 */
export const getAllDep = () =>
  request<Dep[]>('/dep/all').then((res) => res.data)

/**
 * 关注或取消关注操作
 * @param id 对应id
 * @param type 关注类型
 */
export const followOrUnfollow = (id: string, type: FollowType = 'doc') =>
  request('/like', 'POST', { id, type })

/**
 * 首页-关注的医生列表
 * @param params 请求参数
 */
export const getDoctorPage = (params: PageParams) =>
  request<DoctorPage>('/home/page/doc', 'GET', params).then((res) => res.data)

/**
 * 首页-查询推荐/减脂/饮食健康/关注页面--百科文章列表
 * @param params 请求参数
 */
export const getKnowledgePage = (params: KnowledgeParams) =>
  request<KnowledgePage>('/patient/home/knowledge', 'GET', params).then(
    (res) => res.data
  )
